//
//  ZongYiVViewController.swift
//  Swift_MyLive
//
//  Created by admin on 16/9/9.
//  Copyright © 2016年 admin. All rights reserved.
//

import UIKit

let zUrl = "http://cbox.cntv.cn/json2015/fenleierjiye/zongyi/zongyiqiangdangdengduo/index.json"

class ZongYiVViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {
   // @available(iOS 6.0, *)
    
   // public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
        
   // }

    
    var collectionView : UICollectionView?
    var dataArray : NSMutableArray?
    override func viewDidLoad() {
        
        self.title = "综艺"
        self.view.backgroundColor = UIColor.white
        self.dataArray = NSMutableArray()
        self .setCollectionView()
        self.loadWebView()
        
    }
    
    func loadWebView()
    {
        SVProgressHUD.show()
        AppLicationInfo.shareInstance.GET(getUrl: zUrl, parame: nil, isHead: false, success: { (data) in
            
            print(data)
            let dict = data.dictionaryForKey(key: "data")
            let items = dict.arrayForKey(key: "itemList")
            
            for dic in items
            {
                let dataM = DataModel()
                dataM.setModelData(dic: dic as! NSDictionary)
            self.dataArray?.add(dataM)
                
               self.collectionView?.reloadData()
                
                SVProgressHUD.dismiss()
            }
            }) { (data, error) in
                
                SVProgressHUD.dismiss()
        }
    }
    
    func setCollectionView()
    {
        let vcf = UICollectionViewFlowLayout()
        vcf.scrollDirection = .horizontal
        vcf.itemSize = CGSize(width:WIDTH/2-1,height: 130)
//        vcf.minimumInteritemSpacing = 1  //cell 之间的横向间距
//        vcf.minimumLineSpacing = 1      //纵向间距
        
        collectionView = UICollectionView(frame: CGRect(x: 0, y: 64, width: WIDTH, height: HEIGHT-64-49), collectionViewLayout: vcf)
        collectionView?.delegate = self
        collectionView?.dataSource = self
        collectionView?.backgroundColor = UIColor.white
        collectionView?.register(LiveCell().classForCoder, forCellWithReuseIdentifier: "cell")
        self.automaticallyAdjustsScrollViewInsets = false
        self.view.addSubview(collectionView!)

    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return (self.dataArray?.count)!
    }
    
    internal func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        var cell = LiveCell()
        cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! LiveCell
        cell.backgroundColor = UIColor.lightGray.withAlphaComponent(0.3)
        
        let dataM = self.dataArray![indexPath.row] as! DataModel
        cell.setCellData(dataM: dataM)
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        let dataM = self.dataArray![indexPath.row] as! DataModel
        let playVc = PlayerViewController()
        playVc.dataModel = dataM
        print(dataM)
        self.popViewController(controller: playVc)
        
    }
    func popViewController(controller:UIViewController)
    {
        controller.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(controller, animated: true)
    }


}
